﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using iPark.Core.Storage;
using System.IO;
using System.IO.IsolatedStorage;

namespace CreateDatabase
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Storage storage = new Storage();

            // 建立数据库
            storage.CreateDatabase();

            // 插入数据
            string path = "TestDataUTF16.csv";

            IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();

            IsolatedStorageFileStream stream = new IsolatedStorageFileStream(path, FileMode.Open, file);

            using (StreamReader sr = new StreamReader(stream))
            {
                string header = sr.ReadLine();

                while (!sr.EndOfStream)
                {
                    string data = sr.ReadLine();

                    string[] infos = data.Split(',');

                    string name = infos[1];
                    string owner = infos[2];
                    int capacity = int.Parse(infos[3]);
                    int parkingMech = int.Parse(infos[4]);
                    int parkingNonMech = int.Parse(infos[5]);
                    string priceInfo = infos[6];
                    string timeInfo = infos[7];
                    string admin = infos[8];

                    float longitude = float.MinValue;
                    float latitude = float.MinValue;

                    if (!string.IsNullOrEmpty(infos[26]))
                    {
                        longitude = float.Parse(infos[26]);
                    }
                    else if (!string.IsNullOrEmpty(infos[11]))
                    {
                        longitude = float.Parse(infos[11]);
                    }

                    if (!string.IsNullOrEmpty(infos[27]))
                    {
                        latitude = float.Parse(infos[27]);
                    }
                    else if (!string.IsNullOrEmpty(infos[12]))
                    {
                        latitude = float.Parse(infos[12]);
                    }

                    string pinyin = infos[24];
                    string keyword = infos[25];

                    string guid = System.Guid.NewGuid().ToString();

                    //create table "Parking_Info"  (
                    //   "Guid"               nvarchar(64)                    not null,
                    //   "Admin"              nvarchar(64),
                    //   "FullName"           nvarchar(500),
                    //   "ShortName1"         nvarchar(500),
                    //   "ShortName2"         nvarchar(500),
                    //   "ShortName3"         nvarchar(500),
                    //   "Owner"              nvarchar(500),
                    //   "Capacity"           int,
                    //   "ParkingMech"        int,
                    //   "ParkingNonMech"     int,
                    //   "PriceInfo"          nvarchar(1000),
                    //   "TimeInfo"           nvarchar(1000),
                    //   "Longitude_Google"   FLOAT,
                    //   "Latitude_Google"    FLOAT,
                    //   "Longitude_Baidu"    FLOAT,
                    //   "Latitude_Baidu"     FLOAT,
                    //   "ChildAdmin1"        nvarchar(64),
                    //   "ChildAdmin2"        nvarchar(64),
                    //   "Type"               int,
                    //   constraint PK_PARKING_INFO primary key ("Guid")
                    //);
                    StorageData sd = new StorageData()
                    {
                        Guid = guid,
                        Admin = admin,
                        Capacity = capacity,
                        ChildAdmin1 = "",
                        ChildAdmin2 = "",
                        FullName = name,
                        LatitudeBaidu = latitude,
                        LatitudeGoogle = float.MinValue,
                        LongitudeBaidu = longitude,
                        LongitudeGoogle = float.MinValue,
                        Owner = owner,
                        ParkingMech = parkingMech,
                        ParkingNonMech = parkingNonMech,
                        PriceInfo = priceInfo,
                        ShortName1 = pinyin,
                        ShortName2 = keyword,
                        ShortName3 = "",
                        TimeInfo = timeInfo,
                        Type = 0
                    };

                    storage.InsertData(sd);
                }
            }
        }
    }
}